<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {
    
    function __construct()
    {
        parent::__construct();
    }
    
    public function index()
    {
        
        $this->load->model('mealtimes');
        
        $data['title'] = "RMPlanner Home";
        //Stub values here
        $data['data'] = array (
            "name" => $this->session->userdata('name'),
            "login" => $this->session->userdata('username'),
            "role" => get_role($this->session->userdata('role')),
            "response" => $this->session->flashdata('message')
            );

        $tomorrow = mktime(0,0,0,date('n'), date('j') +1);
        $data['data']['tonight'] = $this->mealtimes->getMealsYMD(date('Y'),date('n'),date('j'));
        $data['data']['tomorrow'] = $this->mealtimes->getMealsYMD(date('Y', $tomorrow),date('n', $tomorrow),date('j', $tomorrow));
        
        
        $tonightMeals = $data['data']['tonight'];
        
        if ($tonightMeals->num_rows() == 0){
            $data['data']['ordering'] = 0; // 0 equals no meals
        }
        else{
            $data['data']['ordering'] = 1; // 1 equals meals
        }
        
        $this->load->model('defaults');
        if (!$this->defaults->ordersAllowed()){
            $data['data']['ordering'] = -1; // -1 equals orders are turned off
        }
        
        $mealtime = $this->mealtimes->getMealID(date("Y"),date("m"),date("j"));
        if ($mealtime == null){
            $mealtime_id = $this->mealtimes->create(date("Y"),date("m"),date("j"));
            $mealtime = $this->mealtimes->getMeals($mealtime_id);
        }
        $servetime = strtotime($mealtime->mealtime_servetime);

        if (time() >= $servetime){
            $servetime_text = "Votes are closed: ".unix_to_time($servetime);
        }
        else{
            $servetime_text = "Votes are open: ".unix_to_time($servetime);
        }
        
        $cutoff = strtotime($mealtime->mealtime_cutoff);

        if (time() >= $cutoff){
            $cutoff_text = "Orders are closed: ".unix_to_time($cutoff);
        }
        else{
            $cutoff_text = "Orders are open: ".unix_to_time($cutoff);
        }
        
        
        $data['data']['order'] = $cutoff_text;
        $data['data']['vote'] = $servetime_text;
        
        $data['view'] = "home/view_home";
        $this->load->view('view_template',$data);
    }
}